home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 22 / Cream of the Crop 22.iso / program / cgazv3n4.zip / ROOT-DIR.ZIP / RSE.C < prev   
C/C++ Source or Header  |  1989-05-31  |  2KB  |  54 lines

  1. /***** RSE.C - Redirect standard error to standard out *************
  2.  * Author: Larry Reid                                              *
  3.  * Compilers: Microsoft, Turbo C. Watcom compiles but does not run *
  4.  *            correctly; DeSmet, Lattice, Zortech cannot compile.  *
  5.  * Memory Models: all                                              *
  6.  *                                                                 *
  7.  * Usage: if you ordinarily invoke your program with               *
  8.  *                                                                 *
  9.  *      C>program arg1 arg2 arg3 ...                               *
  10.  *                                                                 *
  11.  *  then invoke it with                                            *
  12.  *                                                                 *
  13.  *      C>rse program arg1 arg2 arg3 ...                           *
  14.  *                                                                 *
  15.  * Source code may be freely used if source is acknowledged.       *
  16.  * Executable form may be freely used. (c) 1988 The C Gazette      *
  17.  *                                                                 *
  18.  *******************************************************************/
  19.  
  20. #include <stdio.h>
  21. #include <process.h>
  22. #include <io.h>
  23.  
  24. extern unsigned char _osmajor;
  25.  
  26. void main(int argc, char **argv)
  27. {
  28.  
  29.     if (_osmajor < 2) {
  30.         fputs("RSE: can't run under DOS 1.x!\n", stderr);
  31.         exit(1);
  32.     }
  33.  
  34.     if (argc < 2) {
  35.         fputs("RSE: nothing to execute!\n", stderr);
  36.         exit(0);
  37.     }
  38.  
  39.     /* Here is the trick: dup2() forces DOS to route references
  40.        to stderr to stdout. The fileno() function returns the
  41.        handle associated with each file.
  42.      */
  43.  
  44.     if (dup2(fileno(stdout),fileno(stderr)) != 0) {
  45.         fputs("RSE: couldn't redirect stderr!\n", stderr);
  46.         exit(1);
  47.     }
  48.  
  49.     /* and now invoke the "real" program with
  50.        what is left of the command line.       */
  51.  
  52.     spawnvp(P_OVERLAY, argv[1], argv+1);
  53. }
  54.